package com.itrip.itripauth.controller;


import com.itrip.common.vo.UserVo;
import com.itrip.itripauth.entity.UserLinkUser;
import com.itrip.itripauth.service.UserLinkUserService;
import com.itrip.itripauth.util.Dto;
import com.itrip.itripauth.util.DtoUtil;
import com.itrip.itripauth.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 四阶段项目二组
 * @since 2020-02-25
 */
@Api("常用旅客信息")
@RestController
@RequestMapping("/biz/api/userinfo")
public class UserLinkUserController {
    @Autowired
    private UserLinkUserService userLinkUserService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("根据旅客名称模糊查询用户信息")
    @PostMapping("queryuserlinkuser")
    public Dto queryuserlinkuser(
            @ApiParam(name = "linkUserName", value = "常用旅客信息", required = false)
            @RequestBody UserLinkUser linkUserName,
            HttpServletRequest request
    ) {

        String tokenString  = request.getHeader("token");
        System.out.println(tokenString);
        if (tokenString!=null) {
        try {
            //查询常用旅客信息的时候是要在当前登录的用户的前提下
            /* redisUtil.get();*/
          /*  String tokenString  = request.getHeader("token");*/

            UserVo userVo= (UserVo) redisUtil.get(tokenString);
            System.out.println(userVo.toString());
            List<UserLinkUser> rowMap = userLinkUserService.selectUserLinkUser(linkUserName.getLinkUserName(), Math.toIntExact(userVo.getId()));
            return DtoUtil.returnDataSuccess(rowMap);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询常用联系人失败");
        }
        }else{
            return DtoUtil.returnFail("token失效，请重新登录！");
        }
    }


    /**
     * 批量和单个删除常用旅客
     *
     * @param ids 常用旅客id
     * @return 返回是否删除失败
     */
    @GetMapping("deluserlinkuser")
    public Dto deluserlinkuser(Integer[] ids) {
        if (ids != null) {
            List<Integer> idsList = Arrays.asList((ids));
            if (userLinkUserService.removeByIds(idsList)) {
                return DtoUtil.returnSuccess("删除常用联系人成功");
            } else {
                return DtoUtil.returnFail("删除常用联系人失败");
            }
        } else {
            return DtoUtil.returnFail("请选择常用联系人");
        }
    }

    @ApiOperation(value = "新增联系人")
    @PostMapping("adduserlinkuser")
    public Dto adduserlinkuser(
            @ApiParam(name = "userLinkUser", value = "对象", required = true)
            @RequestBody UserLinkUser userLinkUser,
            HttpServletRequest request
    ) {
        //新增的时候要填写当前所登录的用户
        String tokenString  = request.getHeader("token");
        if (tokenString!=null) {
            //新增的时候要填写当前所登录的用户
            System.out.println(tokenString);
            UserVo userVo = (UserVo) redisUtil.get(tokenString);
            userLinkUser.setUserId(Math.toIntExact(userVo.getId()));
           /* userLinkUser.setUserId(29);*/
            userLinkUser.setLinkidcardType(0);
            userLinkUserService.save(userLinkUser);
        return DtoUtil.returnSuccess("新增常用联系人成功");
        }else{
            return DtoUtil.returnFail("token失效，请重新登录！");
        }
    }

    @ApiOperation(value = "根据ID修改常用联系人")
    @PostMapping("modifyuserlinkuser")
    public Dto save(
            @ApiParam(name = "userLinkUser", value = "常用联系人对象", required = true)
            @RequestBody UserLinkUser userLinkUser,
            HttpServletRequest request
    ) {
        String tokenString = request.getHeader("token");
        if (tokenString != null) {
        try {
            //新增的时候要填写当前所登录的用户
            System.out.println(tokenString);
            UserVo userVo = (UserVo) redisUtil.get(tokenString);
            //将修改常用人的修改者改成当前所登录的用户
            userLinkUser.setModifiedBy((long) Math.toIntExact(userVo.getId()));
            userLinkUserService.updateById(userLinkUser);
            return DtoUtil.returnSuccess("修改常用联系人成功");
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("修改常用联系人失败");
        }
        }else{
            return DtoUtil.returnFail("token失效，请重新登录！");
        }

    }

}
